import pandas
from vutils.logger import getFileLogger
from vutils.network import Network


class Worker(object):
    def __init__(self, **kwargs):
        self.logger = getFileLogger(kwargs["accountId"])
        self.params = kwargs
        self.token = None

    def http_requests(self, method, path, **kwargs):
        url = self.params["domain"] + path
        self.logger.info(f"HttpRequests: URL-{url}")
        try:
            respond = Network.http_requests(method, url, headers={"Cookie": f"JWToken={self.token}"}, **kwargs)
            data_json, error_message = respond.get("data"), respond.get("message")
            if error_message is not None:
                raise ValueError(error_message)
        except Exception as exp:
            self.logger.error(exp)
        else:
            return data_json

    def get_signals(self):
        respond = self.http_requests(
            method="GET",
            path="/crystal/schedule",
            params={
                "account": account["_id"],
                "strategy": account["strategy"],
                "used": 0
            }
        )
        if not respond:
            return pandas.DataFrame()
        respond = pandas.DataFrame(respond)
        respond["datetime"] = pandas.to_datetime(respond["datetime"])
        return respond

    def login(self):
        respond = Network.http_requests(
            method="POST",
            url=f"{self.params['domain']}/user/login",
            data={
                "username": self.params["username"],
                "password": self.params["password"]
            }
        )
        return respond.get("data")

    def start(self):
        self.token = self.login()


def main():
    pass
